
<%
var a ="<%@LANGUAGE='JAVASCRIPT'>";

var DB = {};

DB.RootDBPath = Server.MapPath("/afterhours/access/BackEnd");

DB.ConnectionStringBase = {
	Access:	"provider=microsoft.jet.oledb.4.0;data source=",
	SQL:	""
};

DB.DBFilePaths = {
	Scripts:			DB.RootDBPath + "\\Scripts.mdb",
	Admin:				DB.RootDBPath + "\\Admin.mdb",
	AfterHours:			DB.RootDBPath + "\\AfterHours_be.mdb",
	Help:				DB.RootDBPath + "\\help.mdb",
	HomeSmartCustomers:	DB.RootDBPath + "\\HomeSmart_Customers.mdb",
	Tickets:			DB.RootDBPath + "\\Tickets.mdb",
	Log:				DB.RootDBPath + "\\Log.mdb",
	Hub:				DB.RootDBPath + "\\DBHub.mdb"
};

DB.ConnectionStrings = {
	Scripts:			DB.ConnectionStringBase.Access + DB.DBFilePaths.Scripts,
	Admin:				DB.ConnectionStringBase.Access + DB.DBFilePaths.Admin,
	AfterHours:			DB.ConnectionStringBase.Access + DB.DBFilePaths.AfterHours,
	Help:				DB.ConnectionStringBase.Access + DB.DBFilePaths.Help,
	HomeSmartCustomers:	DB.ConnectionStringBase.Access + DB.DBFilePaths.HomeSmartCustomers,
	Tickets:			DB.ConnectionStringBase.Access + DB.DBFilePaths.Tickets,
	Log:				DB.ConnectionStringBase.Access + DB.DBFilePaths.Log,
	Hub:				DB.ConnectionStringBase.Access + DB.DBFilePaths.Hub
};

DB.CursorLocation = {
	None:		1,
	Server:		2,
	Client:		3
};

DB.CursorTypes = {
	ForwardOnly: 	0,
	KeySet:			1,
	Dynamic:		2,
	Static:			3
};

DB.LockTypes = {
	ReadOnly:			1,
	Pessimistic:		2,
	Optimistic:			3,
	BatchOptimistic:	4
};

DB.CommandTypes = {
	Text:		1,
	Table:		2,
	StoredProc:	4,
	Unkown:		8,
	File:		256,
	TableDirect:512	
}

DB.DataTypes = {
	Integer:	3,
	Boolean:	11,
	String:		129,
	TimeStamp:	135
}

/*----------------------------------------------------------------------*/
DB.DataCall = function(){
	
	this.ConnectionString =		"";
	this.Connection  =			null;
	this.ManualCloseConnection = false;

	this.SQL =					null;
	this.TableName =			null;
	this.QueryName =			null;
	this.StoredProcName =		null;

	this.Command =				Server.CreateObject("ADODB.Command");
	this.CommandType =			DB.CommandTypes.Text;
	this.CommandText =			"";
	this.Parameters =			[];

	this.RecordSet =			Server.CreateObject("ADODB.Recordset");
	this.CursorLocation =		3;
	this.CursorType =			DB.CursorTypes.ForwardOnly;
	this.LockType =				DB.LockTypes.ReadOnly;
	this.RecordCount =			-1;
	this.ManualCloseRecordSet = false;
	this.DisconnectRecordSet =	true;
	
	this.Success =				false;
	this.ErrorMessage =			"";
	
	/*----------------------------------------------------------------------*/
	this.Execute = function(){
		var rs, conn, cat, cmd, curParam, commandParam, nothing;
		
		try{
			if(this.Connection && this.Connection.State == 1){
				conn = this.Connection;
			}
			else{
				conn = Server.CreateObject("ADODB.Connection");
				conn.Open(this.ConnectionString);
			}
			

			rs =				Server.CreateObject("ADODB.Recordset");
			rs.CursorLocation =	this.CursorLocation;
			rs.CursorType =		this.CursorType;
			rs.LockType =		this.LockType;
			nothing =			rs.ActiveConnection; //this is a trick to get the otherwise ungettable value of Nothing to eventually set the ActiveConnection value back to
			
			if(this.SQL){
				rs.Open(this.SQL, conn);
				
				this.CommandText = rs.Source;
			}
			else{
				cmd =					Server.CreateObject("ADODB.Command");
				cmd.ActiveConnection =	conn;
				cmd.CommandType =		this.CommandType;
				cmd.CommandText =		this.SQL || this.TableName || this.QueryName || this.StoredProcName;
				
				
				if(cmd.CommandType == DB.CommandTypes.StoredProc && this.Parameters.length){
					for(var x = 0, len = this.Parameters.length; x < len; x++){
						curParam = this.Parameters[x];
									
						if(curParam.type == DB.DataTypes.String){
							commandParam = cmd.CreateParameter(curParam.name, curParam.type, curParam.direction || 1, curParam.size || 255, curParam.value);
						}
						else{
							commandParam =			cmd.CreateParameter(param, curParam.type, 1);
							commandParam.Value =	curParam.value;
						}
						
						cmd.Parameters.Append(commandParam);
					}
				}
				
				this.CommandText =	cmd.CommandText;
				this.Command =		cmd;
				
				rs.Open(cmd);
				this.RecordCount = rs.RecordCount;
			}
			

			if(this.DisconnectRecordSet == true){
				rs.ActiveConnection =	nothing;
			}

			this.RecordSet =		rs.Clone(); //have to clone() because of bug in ADO that wont allow disconnected recordsets when rs is using a command for its source
		}
		
		catch(e){
			this.ErrorMessage = "DB.DataCall.Execute() Error:\n";
								
			for(var x = 0, len = conn.Errors.Count; x < len; x++){
				var curError = conn.Errors.Item(x);
				
				this.ErrorMessage += "conn.Error[" + x + "]: " + curError.description + "\n";
				this.Success = false;
			}
		}
		
		finally{
			this.Success = (conn.Errors.Count == 0);
			
			if(conn.State != 0 && !this.ManualCloseConnection){
				conn.Close();
				conn = null;
			}
			
			this.Connection = conn;
			
			
			if(rs && rs.State && rs.State != 0 && !this.ManualCloseRecordSet){
				rs.Close();
				rs = null;
			}
			
			
			if(cmd && cmd.ActiveConnection){
				cmd.ActiveConnection = nothing;
			}
			//cmd = null;
		}
		
	};
	
	/*----------------------------------------------------------------------*/
	this.AddParameter = function(paramName, paramType, paramValue, paramSize, paramDirection){
		var param ={name: paramName, type:  paramType, value: paramValue, size: 255, direction: 1};
		
		if(paramSize){
			param.size = paramSize;
		}
		
		if(paramDirection){
			param.direction = paramDirection;
		}
		
		this.Parameters.push(param);
	}
};





%>
